﻿using game.com;
using System;
using System.Drawing;

//using System.Threading;
using System.Windows.Forms;

namespace game
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //System.Threading.Thread.Sleep(1000);

            sp_block = new spirit.sp_block();
            sp_block.color = Color.Green;
            sp_block.point = new Point(0, 0);
            sp_block.size = 100;
            sp_block.bgcolor = this.BackColor;
            sp_block.graghics = Graphics.FromHwnd(this.Handle);
            sp_block.direction = Direction.Right;
            //sp_block.paint();
            //sp_block.Move();

            t = new Timer();
            t.Interval = 1000;
            t.Tick += t_Tick;
            t.Start();

            //UserControlsnake userControlhttp = new UserControlsnake();
            //userControlhttp.Dock = DockStyle.Fill;
            //this.Width = 800;
            //this.Height = 600;
            //this.Controls.Add(userControlhttp);

            //spirit.sp_block sp_block = new spirit.sp_block();
            //sp_block.color = Color.Green;
            //sp_block.point = new Point(0, 0);
            //sp_block.size = 10;
            ////sp_block.paint(this.CreateGraphics());

            //var gp = Graphics.FromHwnd(this.Handle);
            //gp.Clear(Color.Yellow);
            //sp_block.paint(gp);
        }

        public System.Timers.Timer timer;

        protected override void OnPaint(PaintEventArgs e)
        {
            //spirit.sp_block sp_block = new spirit.sp_block();
            //sp_block.color = Color.Green;
            //sp_block.point = new Point(2, 2);
            //sp_block.size = 100;
            ////sp_block.paint(this.CreateGraphics());

            //var gp = Graphics.FromHwnd(this.Handle);
            ////gp.Clear(Color.Yellow);
            //sp_block.paint(gp);
            base.OnPaint(e);
        }

        private int count;
        private int count1;

        private void OnBlockTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            //throw new NotImplementedException();
            count++;
            //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            this.richTextBox1.Text = count.ToString();
        }

        private Point mouseDownPoint;

        private bool flag;
        private bool isMove;

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            //flag = true;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            //if (flag) pictureBox1.Location = new Point(e.X,e.Y);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            var p = sp_block;
            //MessageBox.Show("11");
            if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up) && p.direction != Direction.Down)
            {
                p.direction = Direction.Up;
                return;
            }
            else if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p.direction != Direction.Left)
            {
                p.direction = Direction.Right;
                return;
            }
            else if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) && p.direction != Direction.Up)
            {
                p.direction = Direction.Down;
                return;
            }
            else if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p.direction != Direction.Right)
            {
                p.direction = Direction.Left;
                return;
            }
        }

        private Timer t;

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private int x;
        private spirit.sp_block sp_block;

        private void t_Tick(object sender, EventArgs e)
        {
            sp_block.Move();
            //count1++;
            //this.richTextBox2.Text = count1.ToString();

            //x++;
            //sp_block = new spirit.sp_block();
            //sp_block.color = Color.Green;
            //sp_block.point = new Point(x, 2);
            //sp_block.size = 100;
            ////sp_block.paint(this.CreateGraphics());

            //var gp = Graphics.FromHwnd(this.Handle);

            //gp.Clear(this.BackColor);
            ////gp.Clear(Color.Yellow);
            //sp_block.paint(gp);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer(1000);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(OnBlockTimedEvent);
            timer.AutoReset = true;
            timer.Start();
        }
    }
}